Ext.ns('journal.home');

// {{{
journal.home.MainToolbar = Ext.extend(Ext.Toolbar, (function() {

    return {

        // configurables
        // anything what is here can be configured from outside

        // {{{
        initComponent : function() {

            var ds = new Ext.data.Store({
                proxy : new Ext.data.ScriptTagProxy({
                    url : 'home?action=search'
                }),

                remoteSort : true, // to enable sorting

                sortInfo : { // the default sort
                    field : 'lastUpdate',
                    direction : 'ASC' | 'DESC'
                },

                listeners : {
                    exception : function(misc) {
                        journal.Utility.showSessionExpired();
                    }

                },

                reader : new Ext.data.JsonReader({
                    root : 'entries',
                    totalProperty : 'totalCount',
                    id : 'entryId'
                }, [ {
                    name : 'entryId',
                    mapping : 'entryId'
                }, {
                    name : 'title',
                    mapping : 'title'
                }, {
                    name : 'lastUpdate',
                    mapping : 'lastUpdate',
                    type : 'date',
                    dateFormat : 'timestamp',
                }, {
                    name : 'summary',
                    mapping : 'summary'
                }, {
                    name : 'treePath',
                    mapping : 'treePath'
                }, {
                    name : 'path',
                    mapping : 'path'
                } ]),
                baseParams : {
                    limit : 20
                }
            });

            // hard coded config - cannot be changed from outside
            var config = {
                height : 27,
                width : '100%',
                items : [
                        {
                            xtype : 'tbsplit',
                            id : journal.Constant.SButton.NEW,
                            iconCls : 'icon-new',
                            handler : function(btn, event) {
                                if (!this.menu.get(journal.Constant.Button.NEW_ENTRY).hidden) {
                                    this.newEntryHandler();
                                } else {
                                    this.newBranchHandler();
                                }
                            },
                            newEntryHandler : function() {
                                var targetNode = Ext.getCmp(journal.Constant.Id.ENTRY_TREE).getSelectionModel()
                                        .getSelectedNode();
                                journal.home.Main.editorPanel.newTabHandler(targetNode);
                            },
                            newBranchHandler : function() {
                                var tree = Ext.getCmp(journal.Constant.Id.ENTRY_TREE);
                                var targetNode = tree.getSelectionModel().getSelectedNode();
                                if (targetNode == null) {
                                    targetNode = tree.root;
                                } else if (targetNode.isLeaf()) {
                                    targetNode = tree.root;
                                }
                                journal.home.EntryExplorerHandler.addFolderHandler(targetNode);
                            },
                            menu : {
                                items : [ {
                                    id : journal.Constant.Button.NEW_ENTRY,
                                    hidden : true,
                                    text : 'New Entry',
                                    iconCls : 'icon-addItem',
                                    handler : function(a, event) {
                                        this.parentMenu.ownerCt.newEntryHandler();
                                    }
                                }, {
                                    id : journal.Constant.Button.NEW_BRANCH,
                                    text : 'New Branch',
                                    iconCls : 'icon-addBranch',
                                    handler : function(a, event) {
                                        this.parentMenu.ownerCt.newBranchHandler();
                                    }
                                } ]
                            }
                        },
                        {
                            xtype : 'tbbutton',
                            id : journal.Constant.Button.SAVE,
                            tooltip : 'Save (Ctrl+S)',
                            iconCls : 'icon-save',
                            disabled : true
                        },
                        '-',
                        'Search: ',
                        ' ',
                        new journal.ui.SearchField({
                            store : ds,
                            width : 160,
                            searchHook : function(text) {
                                if (text.length < 3) {
                                    this.markInvalid('Search query requires at least 3 characters.');
                                } else {
                                    this.clearInvalid();

                                    //seearchTab should throw error because its out of scope but its working.
                                    var searchTab = new journal.home.SearchTab({
                                        searchString : text,
                                        store : this.store
                                    });

                                    journal.home.Main.editorPanel.add(searchTab);
                                    searchTab.show();
                                    journal.home.Main.editorPanel.get('search-tab-singleton').setTitle(
                                            'Search Result for "' + text + '"');

                                }
                            }
                        }), ' ', {
                            xtype : 'tbfill'
                        }, '<a href="' + journal.Constant.REPORT_ISSUE + '" target="_blank">Report an issue</a>', '-',
                        'Version: ' + this.appVersion ]
            };// var config

            // apply config
            Ext.apply(this, Ext.apply(this.initialConfig, config));

            // call parent
            journal.home.MainToolbar.superclass.initComponent.apply(this, arguments);

        } // e/o function initComponent
    // }}}
    };
})()); // e/o extend

// register xtype
Ext.reg('journal.home.MainToolbar', journal.home.MainToolbar);
// }}}
